#! /bin/sh
# Copyright (c) 2002, Intel Corporation. All rights reserved.
# Created by:  inaky.perez-gonzalez REMOVE-THIS AT intel DOT com
# This file is licensed under the GPL license.  For the full content
# of this license, see the COPYING file at the top level of this
# source tree.

# Added FreeBSD compatibility changes by Craig Rodrigues

usage()
{
    cat <<EOF 
Usage: $(basename $0) [OPTIONs] DIRECTORY

Lists the tests (source/binary) available from the DIRECTORY directory
and down.

  --buildable     List only tests that require building
  --execs         List only tests that are executable
                  If you just want to build a test, but not run it,
                  do not include a main function into the .c file or
                  name it something including the "-buildonly" string.
  --fmake         Find functional makefiles.
  --smake         Find stress makefiles.
  --frun          Find functional run.sh files.
  --srun          Find stress run.sh files.
  --help          Show this help and exit

Filenames need to follow some standarized format for them to be picked
up by this tool. This might change in the future. So far, the ones
picked up are:

NUMBER-NUMBER.c     [requires compilation]
NUMBER-NUMBER.sh    [does not require compilation]
NUMBER-buildonly.c  [requires compilation]
NAME.sh             [does not require compilation]

Note that the [requires compilation] tags will mean that the actual
test name for TEST.c after compiling will be TEST. Currently it does
not support TESTs compiled from many different sources.

EOF
}

buildable="";
execs=""
print_execs=0

# Go through the cmd line options
while true
do
  case "$1" in
      "--buildable")
          buildable="( -name [0-9]*-*.c ! -name [0-9]*-[0-9]*.sh )";
          shift;
          ;;
      "--execs")
          print_execs=1;
          execs="( ( -name [0-9]*-[0-9]*.c -o -name [0-9]*-[0-9]*.sh ) -a ! -name *-buildonly* )";
          shift;
          ;;
      "--fmake")
          find functional/ -maxdepth 2 -mindepth 2 -type f -name "Makefile" -exec dirname '{}' ';'
          exit 0;
          ;;
      "--frun")
          find functional/ -maxdepth 2 -mindepth 2 -type f -name "run.sh" -exec dirname '{}' ';' 
          exit 0;
          ;;
      "--smake")
          find stress/ -maxdepth 2 -mindepth 2 -type f -name "Makefile" -exec dirname '{}' ';'
          exit 0;
          ;;
      "--srun")
          find stress/ -maxdepth 2 -mindepth 2 -type f -name "run.sh" -exec dirname '{}' ';'
          exit 0;
          ;;
      "--help")
          usage;
          exit 0;
          ;; 
      --*)
          echo "Unknown option $1" 1>&2;
          usage 1>&2;
          exit 1;
          ;;
      *)
          break 2;
          ;;
  esac
done

# Need the DIRECTORY arg ...
if [ -z "$1" ]
then
    echo "Error: no root directory specified" 1>&2
    usage 1>&2;
    exit 1;
fi

# Simple version right now, just locate all:
WHERE="$1"

# Force something .c or .sh
# Avoid .o, backups
# IF --execs, force it has no "-buildonly"
# If --buildable, remove the .sh files
find "$WHERE" -type f \
    \( \
       \( -name "[0-9]*-*.c" -o -name "[0-9]*-[0-9]*.sh" \) \
       ! -name \*.o ! -name \*~ \
    \) \
    $buildable $execs \
    | if [ $print_execs -eq 1 ]
        then
            sed 's/\.\(sh\|c\)$/.test/' 
        else
            cat
        fi


